package com.javasm.mingming.upload.controller;

import com.alibaba.excel.EasyExcel;
import com.javasm.mingming.adminuser.entity.AdminUser;
import com.javasm.mingming.adminuser.service.AdminUserService;
import com.javasm.mingming.common.utils.ExportUtil;
import com.javasm.mingming.test.entity.TestStudent;
import com.javasm.mingming.test.service.TestStudentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author javasm
 * @version 0.1
 * @className ExportController
 * @descriptioin:
 * @date 2025/1/18 11:35
 * @since jdk11
 */
@RestController
@RequestMapping("/export")
public class ExportController {

    @Resource
    HttpServletResponse response;
    @Resource
    TestStudentService studentService;

    @GetMapping("/stu/excel")
    public void exportStuExcel() throws IOException {
        //获取 需要导出的信息
        List<TestStudent> list = studentService.list();
        //设置响应的头信息
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        //文件名
        String fileName = URLEncoder.encode("学生数据","UTF-8").replaceAll("\\+","%20");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        //使用easyExcel 写入数据
        EasyExcel.write(response.getOutputStream(), TestStudent.class).sheet("数据1").doWrite(list);

        response.getOutputStream().close();
    }
    @Resource
    ExportUtil<TestStudent> exportUtil;
    @GetMapping("/stu/excel2")
    public void exportStuExcel2(){
        List<TestStudent> list = studentService.list();
        exportUtil.expExcel("学生数据",list, TestStudent.class);
    }

    @Resource
    AdminUserService adminUserService;
    @GetMapping("/stu/word1")
    public void exportWord(){
        //应该在Service层调用
        //把一些数据,写入到word里 导出来
        //数据
        AdminUser adminUser = adminUserService.getByUname("test");
        Map<String,Object> map = new HashMap<>();
        map.put("id","10000100999");
        map.put("jia",adminUser);

        map.put("yiname","张三");
        map.put("yiid","333999099099023903209");

        List<TestStudent> studentList = studentService.list();
        map.put("stuList",studentList);

        exportUtil.expWord("测试文件1","demo.xml",map);
    }
}
